--行会主殿功能
--#include "data\config\guild\Guild.lua" once

--行会升级（即主殿升级）
function UpgradeGuild( sysarg, args )
	--print("UpgradeGuild...")
	local guildId 		= Actor.getGuildId(sysarg)
	local guildLevel 	= GetGuildLevel(guildId)
	--print("UpgradeGuild, guildId="..guildId..", guildLevel="..guildLevel)
	if guildId <= 0 or guildLevel <= 0 then
		return
	end

	local nextGuildLevel = guildLevel+1
	local upgradeCfg = GuildConfig.guildUpgrade.levelUpgrade[nextGuildLevel]
	if not upgradeCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild004, ttFlyTip)
		return
	end

	--[[没有CD时限
	local curTime = System.getCurrMiniTime()
	if System.getGuildBuildingCdTime(guildId) > curTime then		--冷却时间未到
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild005,ttFlyTip)
		return
	end
	]]

	local needGuildCoin = upgradeCfg.needGuildCoin
	local guildCoin 	= Actor.getGuildCoin(sysarg)
	--print(" UpgradeGuild, nextGuildLevel="..nextGuildLevel..", needGuildCoin="..needGuildCoin..", guildCoin="..guildCoin)
	if needGuildCoin > guildCoin then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild006, ttFlyTip)
		return
	end

	if not Guild.changeGuildCoin(guildId, -needGuildCoin) then		--减少行会资金
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild006, ttFlyTip)
		return
	end

	System.setGuildBuildingLevel(guildId, gbtMainHall, nextGuildLevel)			--行会等级+1
	local cdTime = upgradeCfg.cdTime or 0
	--System.setGuildBuildingCdTime(guildId, cdTime * 60 + curTime)				--设置cd时间

	Actor.sendGuildInfo(sysarg)
end


--行会捐献
function GuildDonate( sysarg, args )
	local donateType 	= args[2]			--1-捐献金币，2-捐献元宝
	local donateFen 	= args[3]			--分数
	local guildId 		= Actor.getGuildId(sysarg)
	local guildLevel 	= GetGuildLevel(guildId)
	--print("GuildDonate, donateType="..donateType..", donateFen="..donateFen)
	if guildId <= 0 or guildLevel <= 0 then
		return
	end

	local moneyType 	= 0				--捐献需要货币数量
	local addGuildCoin	= 0				--捐献增加行会资金
	local addGuildGx	= 0				--捐献增加本人行会贡献
	local moneyType 	= nil
	local donateEventId = 0 			--行会捐献的事件ID（写死的）
	local logId			= 0
	local donateEventStr= ""
	if donateType == 1  then		--金币捐献
		moneyType 		= mtBindCoin
		moneyCount 		= GuildConfig.donate.donateCoin.needCoin * donateFen
		addGuildCoin	= GuildConfig.donate.donateCoin.addGuildCoin* donateFen
		addGuildGx		= GuildConfig.donate.donateCoin.addGuildGx* donateFen
		donateEventId 	= 1361			--1361是 tpGuildEventDonateCoin，固定不会变
		donateEventStr  = Lang.ScriptTips.GuildEvent001
		logId			= GameLog.clGuildDonateCoin
	elseif donateType == 2 then		--元宝捐献
		moneyType 		= mtYuanbao
		moneyCount 		= GuildConfig.donate.donateYuanBao.needYuanBao * donateFen
		addGuildCoin	= GuildConfig.donate.donateYuanBao.addGuildCoin* donateFen
		addGuildGx		= GuildConfig.donate.donateYuanBao.addGuildGx* donateFen
		donateEventId 	= 1362			--1362是 tpGuildEventDonateYuanBao，固定不会变
		donateEventStr  = Lang.ScriptTips.GuildEvent002
		logId			= GameLog.clGuildDonateYuanBao
	else
		return
	end

	if moneyType ~= mtBindCoin and moneyType ~= mtYuanbao then 		--既不是金币、也不是元宝
		return
	end
	--print("GuildDonate, moneyType="..moneyType..", moneyCount="..moneyCount..", addGuildCoin="..addGuildCoin..", addGuildGx="..addGuildGx)

	if moneyCount <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild001, ttFlyTip)
		return
	end

	local todayDonateCoin = 0
	if moneyType == mtBindCoin then		--金币捐献
		todayDonateCoin 	= Actor.getGuildTodayDonateCoin(sysarg)	--今日已经捐献金币的数量
		local donateLimit 	= GuildConfig.dailyDonateLimit[guildLevel]
		if not donateLimit then
			return
		end

		local coinLimit = donateLimit.coinLimit
		local guildPos = Actor.getGuildPos(sysarg)
		if guildPos == smGuildLeader then 			--会长双倍限额
			coinLimit = 2 * donateLimit.coinLimit
		end
		--print("GuildDonate, donateType="..donateType..", coinLimit="..coinLimit)
		if todayDonateCoin >= coinLimit or todayDonateCoin + moneyCount > coinLimit then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild003, ttFlyTip)
			return
		end
	end
	
	--[[
	local hasMoneyCount = Actor.getMoneyCount( sysarg, moneyType )    --mtBindCoin这里是金币
	if hasMoneyCount < moneyCount then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild002, ttFlyTip)
		return
	end

	if not Actor.changeMoney(sysarg, moneyType, -moneyCount, GameLog.clGuildDonate, Lang.ScriptTips.GuildLog001) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild002, ttFlyTip)
		return
	end
	]]

	if not CheckConsumeMoney( sysarg, moneyType, moneyCount, 1 ) then
		return
	end

	if not DoConsumeMoney( sysarg, moneyType, moneyCount, 1, logId, Lang.ScriptTips.GuildLog001 ) then
		return
	end

	Guild.changeGuildCoin(guildId, addGuildCoin)		--增加行会资金
	Actor.changeGuildGx(sysarg, addGuildGx)				--增加本人的行会贡献

	if moneyType == mtBindCoin then		--金币捐献
		todayDonateCoin = todayDonateCoin + moneyCount
		Actor.setGuildTodayDonateCoin(sysarg, todayDonateCoin)
		--print("GuildDonate, todayDonateCoin="..todayDonateCoin)
	end

	local actorName = Actor.getName( sysarg )
	local eventMsg = string.format( donateEventStr, actorName, moneyCount )
	Guild.addEventRecord( guildId, eventMsg, donateEventId, moneyCount, 0, 0, actorName, "" )
end

--[[
行会祝福
]]
function GuildBless( sysarg )
	local guildId 		= Actor.getGuildId(sysarg)
	local guildLevel 	= GetGuildLevel(guildId)
	if guildId <= 0 or guildLevel <= 0 then
		return
	end

	local guildBlessLevel = Actor.getGuildBlessLevel( sysarg )			--行会祝福等级
	--print("GuildBless, guildBlessLevel="..guildBlessLevel..", guildLevel="..guildLevel)
	if guildBlessLevel >= guildLevel then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild007, ttFlyTip)
		return
	end

	local nextGuildBlessLevel = guildBlessLevel + 1 
	local blessCfg = GuildConfig.guildBless[nextGuildBlessLevel]
	if not blessCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild008, ttFlyTip)
		return
	end

	if blessCfg.needGx <= 0 then
		return
	end

	if Actor.getGuildGx( sysarg ) < blessCfg.needGx then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild009, ttFlyTip)
		return
	end

	if not Actor.changeGuildGx(sysarg, -blessCfg.needGx) then			--扣除本人的行会贡献
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild009, ttFlyTip)
		return
	end

	Actor.setGuildBlessLevel( sysarg, nextGuildBlessLevel )
	Actor.collectOperate(sysarg, 0)						--刷属性
end

--[[
登陆（不在线跨天）
行会祝福等级衰减
]]
function ProcGuildBlessLoginIn( sysarg )
	local guildBlessLevel = Actor.getGuildBlessLevel( sysarg )			--行会祝福等级为0，直接返回
	if guildBlessLevel <= 0 then
		return
	end

	local guildId 	 = Actor.getGuildId(sysarg)
	local guildLevel = GetGuildLevel(guildId)
	if guildId <= 0 or guildLevel <= 0 then				--没有行会，祝福清0
		Actor.setGuildBlessLevel( sysarg, 0 )
		Actor.collectOperate(sysarg, 0)
		return
	end

	local logoutDays = Actor.getLastLogoutDays( sysarg )
	if logoutDays <= 0 then
		return
	end

	if logoutDays >= guildBlessLevel then
		Actor.setGuildBlessLevel( sysarg, 0 )
	else
		Actor.setGuildBlessLevel( sysarg, guildBlessLevel - logoutDays )
	end
	--local guildBlessLevelNew = Actor.getGuildBlessLevel( sysarg )			--行会祝福等级
	--print("ProcGuildBlessLoginIn, guildBlessLevel="..guildBlessLevel..", logoutDays="..logoutDays..", guildBlessLevelNew="..guildBlessLevelNew)
	Actor.collectOperate(sysarg, 0)
end

--[[
在线跨天（不包括登陆）
]]
function ProcGuildBlessNewDay( sysarg )
	local guildBlessLevel = Actor.getGuildBlessLevel( sysarg )			--行会祝福等级
	if guildBlessLevel <= 0 then
		return
	end

	local guildId 		= Actor.getGuildId(sysarg)
	local guildLevel 	= GetGuildLevel(guildId)
	if guildId <= 0 or guildLevel <= 0 then				--没有行会，祝福清0
		Actor.setGuildBlessLevel( sysarg, 0)
		Actor.collectOperate(sysarg, 0)
		return
	end

	Actor.setGuildBlessLevel( sysarg, guildBlessLevel - 1 )
	--local guildBlessLevelNew = Actor.getGuildBlessLevel( sysarg )			--行会祝福等级
	--print("ProcGuildBlessNewDay, guildBlessLevel="..guildBlessLevel..", guildBlessLevelNew="..guildBlessLevelNew)
	Actor.collectOperate(sysarg, 0)
end

--[[获取行会等级 
不要使用：Actor.getGuildLevel(sysarg)
]]
function GetGuildLevel(guildId)
	return System.getGuildbuildingLevel(guildId, gbtMainHall)
end


--[[
是否能发起行会速传
相关消息（10 57）
]]
function CheckStartGuildTeleport( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		return false
	end

	if Actor.hasMapAreaAttri(sysarg, aaNotGuildTran) then 			--场景区域禁止发起行会传送
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild011, ttFlyTip)
		return
	end

	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.guildTeleTime then
		dVar.guildTeleTime = 0
	end

	local now = System.getCurrMiniTime()
	if now - dVar.guildTeleTime < GuildConfig.guildTeleport.startTeleport.cdTime then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild010, ttFlyTip)
		return
	end

	local consume 		= GuildConfig.guildTeleport.startTeleport.consume
	local unitConsume	= GuildConfig.guildTeleport.startTeleport.unitConsume
	if not consume then
		return false
	end

	if not CheckConsumeCondWithReplace( sysarg, consume, unitConsume ) then
		return false
	end

	if not DoConsumeCondWithReplace( sysarg, consume , unitConsume, GameLog.clStartGuildTeleportFee, "start tele"  ) then
		return false
	end

	dVar.guildTeleTime = now		--后面发起传送应该是能成功的
  	return true
end


--[[
是否能进行行会速传
相关消息（10 58）
]]
function CheckDoGuildTeleport( sysarg, teleId, nTeleTime )
	--print(" CheckDoGuildTeleport, teleId="..teleId..", nTeleTime="..nTeleTime)
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		return false
	end

	--判断能否进行传送
	if not Actor.canTelport( sysarg, 0 ) then			--是否允许传送
		return false
	end

	--判断能否进行行会传送
	if Actor.hasMapAreaAttri(sysarg, aaNotBeGuildTran) then 			--场景区域禁止进行行会传送
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Guild012, ttFlyTip)
		return false
	end

	local consume = GuildConfig.guildTeleport.doTeleport.consume
	if not consume then
		return false
	end

	if not CheckConsumeCond( sysarg, { consume } ) then
		return false
	end

	if not DoConsumeCond( sysarg, { consume }, 1, GameLog.clDoGuildTeleportFee, "do tele"  ) then
		return false
	end

  	return true
end
